using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentAttendanceTypeDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentAttendanceType objects.
    /// </summary>
    public class StudentAttendanceTypeDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentAttendanceType from the underlying datasource.
        /// </summary>
        /// <param name="attendanceTypeCode">The unique AttendanceTypeCode of the StudentAttendanceType in the database.</param>
        /// <returns>An StudentAttendanceType when the AttendanceTypeCode was found in the database, or null otherwise.</returns>
        public static StudentAttendanceType GetItem(short attendanceTypeCode)
        {
            StudentAttendanceType myStudentAttendanceType = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTypeSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@attendanceTypeCode", attendanceTypeCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAttendanceType = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAttendanceType;
            }
        }

        /// <summary>
        /// Returns a list with StudentAttendanceType objects.
        /// </summary>
        /// <returns>A generics List with the StudentAttendanceType objects.</returns>
        public static StudentAttendanceTypeList GetList()
        {
            StudentAttendanceTypeList tempList = new StudentAttendanceTypeList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTypeSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentAttendanceType in the database.
        /// </summary>
        /// <param name="myStudentAttendanceType">The StudentAttendanceType instance to save.</param>
        /// <returns>The new AttendanceTypeCode if the StudentAttendanceType is new in the database or the existing AttendanceTypeCode when an item was updated.</returns>
        public static int Save(StudentAttendanceType myStudentAttendanceType)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTypeInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentAttendanceType.AttendanceTypeCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@attendanceTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@attendanceTypeCode", myStudentAttendanceType.AttendanceTypeCode);
                    }
                    if (string.IsNullOrEmpty(myStudentAttendanceType.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myStudentAttendanceType.Description);
                    }
                    if (myStudentAttendanceType.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myStudentAttendanceType.Status);
                    }
                    if (myStudentAttendanceType.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myStudentAttendanceType.CreatedDateTime);
                    }
                    if (myStudentAttendanceType.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentAttendanceType.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentAttendanceType from the database.
        /// </summary>
        /// <param name="attendanceTypeCode">The AttendanceTypeCode of the StudentAttendanceType to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short attendanceTypeCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTypeDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@attendanceTypeCode", attendanceTypeCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentAttendanceType class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentAttendanceType FillDataRecord(IDataRecord myDataRecord)
        {
            StudentAttendanceType myStudentAttendanceType = new StudentAttendanceType();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AttendanceTypeCode")))
            {
                myStudentAttendanceType.AttendanceTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AttendanceTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myStudentAttendanceType.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentAttendanceType.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentAttendanceType.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentAttendanceType.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentAttendanceType;
        }
    }

}
